﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using Model;
using System.IO;
using GameHelp;

namespace TdyxAutoBot
{
    public partial class AccountForm : Form
    {
        private AddAcountForm addAcountForm = null;
        private TimeCloseWindow timeMessageFrom = null;
        public AccountForm()
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
        }

        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏
        }

        //添加账户
        private void button1_Click(object sender, EventArgs e)
        {
            if (addAcountForm == null)
            {
                addAcountForm = new AddAcountForm();
                addAcountForm.OnAddOK += new AddAcountFormOK(addAcountForm_OnAddOK);
            }
            addAcountForm.ShowAdd(Manager.AccountList);
        }
        //保存数据
        private void buttonSave_Click(object sender, EventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//config/account.xml";
            using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite,FileShare.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Account>));
                serializer.Serialize(stream, Manager.AccountList);                
                stream.Close();
            }

            if (timeMessageFrom == null)
            {
                timeMessageFrom = new TimeCloseWindow();                
            }
            timeMessageFrom.Show("账户已保存。");
        }

        //第一次显示，加载账户信息
        private void AccountForm_Shown(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = Manager.AccountList;
        }

        //删除和修改
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "操作")
            {
                if (this.dataGridView1.SelectedCells[0].Value.Equals("修改"))
                {
                    if (addAcountForm == null)
                    {
                        addAcountForm = new AddAcountForm();
                        addAcountForm.OnEditOK += new AddAcountFormOK(addAcountForm_OnEditOK);
                    }
                    addAcountForm.ShowEdit(Manager.AccountList[e.RowIndex]);
                }
                else if (this.dataGridView1.SelectedCells[0].Value.Equals("删除"))
                {
                    DialogResult result = MessageBox.Show("删除后程序将不再监管这个账户，您真的要删除这个账户吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        this.dataGridView1.DataSource = null;
                        Manager.AccountList.RemoveAt(e.RowIndex);
                        this.dataGridView1.DataSource = Manager.AccountList;
                    }
                }
                else if (this.dataGridView1.SelectedCells[0].Value.Equals("配置"))
                {
                    new ConfigForm().Show(Manager.AccountList[e.RowIndex]);
                }
            }
        }

        void addAcountForm_OnAddOK(Model.Account account)
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = Manager.AccountList;
            this.dataGridView1.Update();
            buttonSave_Click(null, null);//保存数据
        }

        void addAcountForm_OnEditOK(Model.Account account)
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = Manager.AccountList;
            this.dataGridView1.Update();
            buttonSave_Click(null, null);//保存数据
        }
    }
}
